$(document).ready(function() {
    dataSource = new kendo.data.DataSource({
        transport: {
            create: {
                url: appBosch.crud.create,
                dataType: "json"
            },
            read: {
                url: appBosch.crud.read,
                dataType: "json"
            },
            update: {
                url: appBosch.crud.update,
                dataType: "json"
            },
            destroy: {
                url: appBosch.crud.destroy,
                dataType: "json"
            },
            parameterMap: function(options, operation) {
                var params = {};
                switch (operation)
                {
                    case 'create':
                        params = {models: kendo.stringify(options.models)};
                        break;
                    case 'read':
                        break;
                    case 'update':
                        params = {models: kendo.stringify(options.models)};
                        break;
                    case 'destroy':
                        params = {id: options.models[0].id};
                        break;
                }
                return params;
            }
        },
        batch: true,
        schema: {
            model: {
                id: "id",
                fields: {
                    user_id: {
                        defaultValue: appBosch.userId,
//                        editable: true,
                        nullable: false
//                        validation: {
//                            required: true,
//                            min: 1
//                        }
                    },
                    line_id: {
                        editable: true,
                        nullable: false,
                        defaultValue: appBosch.lines[0].value
                    }
                }
            }
        }
    });

    $("#grid").kendoGrid({
        scrollable: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        sortable: {
            mode: "single",
            allowUnsort: false
        },
        pageable: true,
        dataSource: dataSource,
        height: 520,
        toolbar: kendo.template($("#templateToolbar").html()),
        columns: [
            {
                field: "user_id",
                title: "Usuario",
                values: appBosch.users,
                width: 450
            },
            {
                field: "line_id",
                title: "Linea",
                values: appBosch.lines,
                width: 200
            },            
            {command: [
                    {
                        name: "edit",
                        text: {
                            update: "Actualizar",
                            cancel: "Cancelar"
                        }
                    },
                    {
                        name: "destroy",
                        text: "Eliminar"
                    }], title: "&nbsp;"}],
        editable: {
            confirmation: "¿Estas seguro que deseas eliminar al usuario de está linea?",
            mode: "inline"
        }
    });

    $("#origen").kendoDropDownList({
        dataTextField: "text",
        dataValueField: "value",
        dataSource: appBosch.users2,
        value: appBosch.userId,
        change: function() {
            var value = this.value();
            var url = appBosch.urlUsers + '/' + value;
            $(location).attr('href', url);
        }
    });
});

